package com.equipment;

import java.util.NoSuchElementException;
import android.annotation.SuppressLint;
import com.util.EncodingUtil;

/**
 * B3
 *
 * @version 1.18
 * @author 刘亚东
 *
 */
@SuppressLint("DefaultLocale")
public class SearchOBUResponse {
	// 55AA03004BDFB30003FFFFFF2200071AB1B1BEA9B1B1BEA9001211010606010001112012080120200101211610110101020304050607081999010120991231BEA94A4C3935323700000000000400FFFFFF09
	// 帧开始标记
	private String stx = "55AA";
	// 串口帧序列号
	private String rsctl = "00";
	// 长度
	private String Length = "";
	// 代码类型
	private String code = "DF";
	// 拓展子码
	private String subCode = "B3";
	// 异或校验值
	private String bcc = "";

	// 状态 0表示找到OBU，非0表示未找到OBU
	private String status = "";
	// OBU MAC地址
	private String obuMac = "";
	// OBU状态 OBUStatus的定义如表1-1
	private String OBUStatus = "";
	// 信息位标志，Bit0：系统信息（0：无，1：有）；Bit1：CPU卡15文件信息（0：无，1：有）；Bit2：卡余额（0：无，1：有）；Bit3：11和12版本的复位信；Bit4：返回8字节OBU随机数用于计算认证码（双向认证）
	private String InfoFlag = "";
	// 系统信息长度，由InfoFlag决定是否有该位
	private String systemInfoLen = "";
	// 系统信息
	private String sysInfo = "";
	// 卡片信息长度
	private String CardInfoLen = "";
	// 卡片信息
	private String CardInfo;
	// CPU卡钱包信息长度
	private String CardRestMoneyLen = "";
	// CPU卡钱包信息
	private String CardRestMoney = "";
	// OBU复位信息长度
	private String OBUResetInfoLen = "";
	// OBUResetInfo
	private String OBUResetInfo = "";
	// 随机数长度
	private String OBURandLen = "";
	// 随机数
	private String OBURand = "";

	// 标签号码
	private String OBUNumber = "";
	// OBU合同版本
	private String ContractVersion = "";
	// 8字节卡片编号
	private String cardNumber = "";
	// 卡片版本
	private String cardConver = "";
	// 车卡绑定
	private String isVehicleCardBinding = "";
	// 卡内车牌号
	private String cardVehicleNumber;

	// OBU签署日期
	private String SignDate = "";
	// 合同过期日期4字节
	private String dueDate = "";
	// 拆卸状态1字节
	private String dismantle = "";

	/**
	 * 解析BA指令
	 *
	 * @param context
	 * @return
	 */
	public SearchOBUResponse analysis(String context)
			throws NoSuchElementException {
		SearchOBUResponse dr = null;
		try {
			// 判断是否是BA指令
			if (!context.substring(12, 14).toUpperCase().equals("B3")) {
				throw new NoSuchElementException("寻不到OBU");
			}
			// 是否找到OBU
			if (!context.substring(14, 16).equals("00")) {
				throw new NoSuchElementException("寻不到OBU");
			}
			// 截取到BCC校验值 需要判断校验是否相等
			bcc = context.substring(context.length() - 2).toUpperCase();
			// 截取到发送的数据(总长度-2个字节计算到数据的最后位置)并转换下FE和FF
			String str = context.substring(4, context.length() - 2);
			// 得到比对校验
			str = EncodingUtil.yihuo(EncodingUtil.change(str)).toUpperCase();
			// 判断校验是否一致
			if (!str.equals(bcc)) {
				throw new NoSuchElementException("寻不到OBU");
			}
			// 如果没问题就new 对象 把解析的值放入对象
			dr = new SearchOBUResponse();
			// 解析OBUMAC
			obuMac = context.substring(16, 24);
			dr.setObuMac(obuMac);
			// 解析OBUStatus
			OBUStatus = context.substring(24, 28);
			dr.setOBUStatus(OBUStatus);
			// 解析InfoFlag
			InfoFlag = context.substring(28, 30);
			dr.setInfoFlag(InfoFlag);

			// InfoFlag 8bit判断信息位标志，
			InfoFlag = context.substring(28, 30);

			// Bit0：系统信息（0：无，1：有）；Bit1：CPU卡15文件信息（0：无，1：有）；Bit2：卡余额（0：无，1：有）；Bit3：11和12版本的复位信；Bit4：返回8字节OBU随机数用于计算认证码（双向认证）
			int i = Integer.parseInt(InfoFlag, 16);
			String flag = EncodingUtil.jointZero(Integer.toBinaryString(i), 8);
			int length;

			// 判断是否有系统信息
			if (flag.substring(7).equals("0")) {
				dr.setSystemInfoLen("0");
				length = 30;
			} else {
				// 解析系统信息长度
				systemInfoLen = context.substring(30, 32);
				dr.setSystemInfoLen(systemInfoLen);
				length = EncodingUtil.hexStringToInt(systemInfoLen);
				length = (length * 2) + 32;
				// 解析系统信息
				sysInfo = context.substring(32, length);
				dr.setSysInfo(sysInfo);
				// 标签号码
				OBUNumber = dr.getSysInfo().substring(20, 36);
				dr.setOBUNumber(OBUNumber);
				// OBU合同版本号
				ContractVersion = dr.getSysInfo().substring(18, 20);
				dr.setContractVersion(ContractVersion);
			}
			int len = 0;
			// 判断OBU是否有卡信息
			if (flag.substring(6, 7).equals("0")) {
				dr.setCardInfoLen("0");
			} else {
				// 卡片信息长度
				CardInfoLen = context.substring(length, length += 2);
				dr.setCardInfo(CardInfoLen);
				len = (EncodingUtil.hexStringToInt(CardInfoLen) * 2) + length;
				// 卡片信息
				CardInfo = context.substring(length, len);
				dr.setCardInfo(CardInfo);
				// 卡片
				cardNumber = CardInfo.substring(4, 8 + 16);
				dr.setCardNumber(cardNumber);
				length = len;

			}
			// 判断OBU是否有余额信息
			if (flag.substring(5, 6).equals("0")) {
				dr.setCardRestMoneyLen("0");
			} else {
				// 钱包长度
				CardRestMoneyLen = context.substring(length, length += 2);
				dr.setCardInfoLen(CardRestMoneyLen);

				len = (EncodingUtil.hexStringToInt(CardRestMoneyLen) * 2)
						+ length;
				// 钱包信息
				CardRestMoney = context.substring(length, len);
				dr.setCardRestMoney(CardRestMoney);

				length = len;
			}

			// 判断是否有复位信息
			if (flag.substring(4, 5).equals("0")) {
				dr.setOBUResetInfoLen("0");
			} else {
				// 复位信息长度
				OBUResetInfoLen = context.substring(length, length += 2);
				dr.setOBUResetInfoLen(OBUResetInfoLen);
				len = (EncodingUtil.hexStringToInt(OBUResetInfoLen) * 2)
						+ length;
				// 复位信息
				OBUResetInfo = context.substring(length, len);
				dr.setOBUResetInfo(OBUResetInfo);
				length = len;
			}
			// 判断是否有随机数信息
			if (flag.substring(3, 4).equals("0")) {
				dr.setOBURandLen("0");
			} else {
				// OBU随机数长度
				OBURandLen = context.substring(length, length += 2);
				dr.setOBURandLen(OBURandLen);
				len = (EncodingUtil.hexStringToInt(OBURandLen) * 2) + length;
				// 随机数
				OBURand = context.substring(length, len);
				dr.setOBURand(OBURand);
			}
		} catch (Exception e) {
			throw new NoSuchElementException("寻不到OBU");
		}

		return dr;
	}

	public String getStx() {
		return stx;
	}

	public void setStx(String stx) {
		this.stx = stx;
	}

	public String getRsctl() {
		return rsctl;
	}

	public void setRsctl(String rsctl) {
		this.rsctl = rsctl;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getSubCode() {
		return subCode;
	}

	public void setSubCode(String subCode) {
		this.subCode = subCode;
	}

	public String getBcc() {
		return bcc;
	}

	public void setBcc(String bcc) {
		this.bcc = bcc;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getObuMac() {
		return obuMac;
	}

	public void setObuMac(String obuMac) {
		this.obuMac = obuMac;
	}

	public String getOBUStatus() {
		return OBUStatus;
	}

	public void setOBUStatus(String oBUStatus) {
		OBUStatus = oBUStatus;
	}

	public String getInfoFlag() {
		return InfoFlag;
	}

	public void setInfoFlag(String infoFlag) {
		InfoFlag = infoFlag;
	}

	public String getSystemInfoLen() {
		return systemInfoLen;
	}

	public void setSystemInfoLen(String systemInfoLen) {
		this.systemInfoLen = systemInfoLen;
	}

	public String getSysInfo() {
		return sysInfo;
	}

	public void setSysInfo(String sysInfo) {
		this.sysInfo = sysInfo;
	}

	public String getLength() {
		return Length;
	}

	public void setLength(String length) {
		Length = length;
	}

	public String getOBUNumber() {
		return OBUNumber;
	}

	public void setOBUNumber(String oBUNumber) {
		OBUNumber = oBUNumber;
	}

	public String getContractVersion() {
		return ContractVersion;
	}

	public void setContractVersion(String contractVersion) {
		ContractVersion = contractVersion;
	}

	public String getOBUResetInfoLen() {
		return OBUResetInfoLen;
	}

	public void setOBUResetInfoLen(String oBUResetInfoLen) {
		OBUResetInfoLen = oBUResetInfoLen;
	}

	public String ContractVersion() {
		return OBUResetInfo;
	}

	public void setOBUResetInfo(String oBUResetInfo) {
		OBUResetInfo = oBUResetInfo;
	}

	public String getOBUResetInfo() {
		return OBUResetInfo;
	}

	public String getCardInfoLen() {
		return CardInfoLen;
	}

	public void setCardInfoLen(String cardInfoLen) {
		CardInfoLen = cardInfoLen;
	}

	public String getCardInfo() {
		return CardInfo;
	}

	public void setCardInfo(String cardInfo) {
		CardInfo = cardInfo;
	}

	public String getCardRestMoneyLen() {
		return CardRestMoneyLen;
	}

	public void setCardRestMoneyLen(String cardRestMoneyLen) {
		CardRestMoneyLen = cardRestMoneyLen;
	}

	public String getCardRestMoney() {
		return CardRestMoney;
	}

	public void setCardRestMoney(String cardRestMoney) {
		CardRestMoney = cardRestMoney;
	}

	public String getOBURandLen() {
		return OBURandLen;
	}

	public void setOBURandLen(String oBURandLen) {
		OBURandLen = oBURandLen;
	}

	public String getOBURand() {
		return OBURand;
	}

	public void setOBURand(String oBURand) {
		OBURand = oBURand;
	}

	public String getCardNumber() {
		return cardNumber;
	}

	public String getCardConver() {
		return cardConver;
	}

	public void setCardConver(String cardConver) {
		this.cardConver = cardConver;
	}

	public void setCardNumber(String cardNumber) {
		this.cardNumber = cardNumber;
	}

	public String getCardVehicleNumber() {
		return cardVehicleNumber;
	}

	public void setCardVehicleNumber(String cardVehicleNumber) {
		this.cardVehicleNumber = cardVehicleNumber;
	}

	public String getIsVehicleCardBinding() {
		return isVehicleCardBinding;
	}

	public void setIsVehicleCardBinding(String isVehicleCardBinding) {
		this.isVehicleCardBinding = isVehicleCardBinding;
	}

	public String getSignDate() {
		return SignDate;
	}

	public void setSignDate(String signDate) {
		SignDate = signDate;
	}

	public String getDueDate() {
		return dueDate;
	}

	public void setDueDate(String dueDate) {
		this.dueDate = dueDate;
	}

	public String getDismantle() {
		return dismantle;
	}

	public void setDismantle(String dismantle) {
		this.dismantle = dismantle;
	}

}
